﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using NovoSerDesEva;
using NovoUI.UserCtrl;
using Sunny.UI;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using static NovoSerDes.Config;

namespace NovoSerDes
{
    public partial class BandwidthCalculator : UIPage
    {
        BandwidthCamera childBandwidthCamera;
        BandwidthConChannel childBandwidthConChannel;
        private double GMSL_LDR = 0;
        private double free = 0; //空闲
        private double total = 0; //GMSL_LDR - 空闲
        private double totalCC = 0;

        private int flagTypePanels = 0;
        private double videoBW = 0; //正向图像
        private double videoBWX = 0;
        private double videoBWY = 0;
        private double videoBWZ = 0;
        private double videoBWU = 0;

        private double forwardGPIO = 0; //正向GPIO
        private double reverseGPIO = 0; //反向GPIO

        //private double reverseI2CPass = 0; //反向I2C(透传模式)
        //private double reverseI2CNonPass = 0; //反向I2C(非透传模式)
        //private double reverseSPI = 0; //反向SPI

        private double reverseI2CAPass = 0; //反向I2C(透传模式)
        private double reverseI2CANonPass = 0; //反向I2C(非透传模式)
        private double reverseSPIA = 0; //反向SPI

        private double reverseI2CBPass = 0; //反向I2C(透传模式)
        private double reverseI2CBNonPass = 0; //反向I2C(非透传模式)
        private double reverseSPIB = 0; //反向SPI

        private double reverseI2CCPass = 0; //反向I2C(透传模式)
        private double reverseI2CCNonPass = 0; //反向I2C(非透传模式)
        private double reverseSPIC = 0; //反向SPI


        public BandwidthCalculator()
        {
            InitializeComponent(); 
            DGVCalInit();
            BindData();
            UIInit();
        }

        public void CalculatorAll()
        {
            List<BandwidthCameraPanel> TypePanels = childBandwidthCamera.GetTypePanels();
            int whichType = 0;
            //目前只看第一个TypePanels[0]
            if (TypePanels.Count > 0)
            {
                flagTypePanels = 1;
                if (TypePanels[0].WhichType == 0)
                {
                    whichType = 0;
                }
                else if(TypePanels[0].WhichType == 1)
                {
                    whichType = 1;
                }
                else if (TypePanels[0].WhichType == 2)
                {
                    whichType = 2;
                }
                else if (TypePanels[0].WhichType == 3)
                {
                    whichType = 3;
                }
            }
            else
            {
                flagTypePanels = 0;
            }
            videoBW = flagTypePanels * (childBandwidthCamera.CamTotalMpbs / 0.75);
            switch (whichType)
            {
                case 0:
                    videoBWX = videoBW;
                    videoBWY = 0;
                    videoBWZ = 0;
                    videoBWU = 0;
                    break;
                case 1:
                    videoBWY = videoBW;
                    videoBWX = 0;
                    videoBWZ = 0;
                    videoBWU = 0;
                    break;
                case 2:
                    videoBWZ = videoBW;
                    videoBWX = 0;
                    videoBWY = 0;
                    videoBWU = 0;
                    break;
                case 3:
                    videoBWU = videoBW;
                    videoBWX = 0;
                    videoBWY = 0;
                    videoBWZ = 0;
                    break;
            }
            //forwardGPIO = childBandwidthConChannel.ConGPIOMpbs * 128 / 0.80;
            //reverseGPIO = childBandwidthConChannel.ConGPIOMpbs * 128 / 0.75;
            forwardGPIO = childBandwidthConChannel.ForwardGPIO;
            reverseGPIO = childBandwidthConChannel.ReverseGPIO;
            //reverseI2CPass = 400 * 8 / 0.75;
            //reverseI2CNonPass = 400 * 2.5 / 0.75;
            //reverseSPI = 400 * 2.5 / 0.75;
            reverseI2CAPass = childBandwidthConChannel.ReverseI2CAPass;
            reverseI2CANonPass = childBandwidthConChannel.ReverseI2CANonPass;
            reverseSPIA = childBandwidthConChannel.ReverseSPIA;

            reverseI2CBPass = childBandwidthConChannel.ReverseI2CBPass;
            reverseI2CBNonPass = childBandwidthConChannel.ReverseI2CBNonPass;
            reverseSPIB = childBandwidthConChannel.ReverseSPIB;

            reverseI2CCPass = childBandwidthConChannel.ReverseI2CCPass;
            reverseI2CCNonPass = childBandwidthConChannel.ReverseI2CCNonPass;
            reverseSPIC = childBandwidthConChannel.ReverseSPIC;
           
            totalCC = forwardGPIO + reverseGPIO + reverseI2CAPass + reverseI2CANonPass + reverseSPIA +
                    reverseI2CBPass + reverseI2CBNonPass + reverseSPIB + reverseI2CCPass + reverseI2CCNonPass + reverseSPIC;
            total = videoBW + totalCC;
            free = GMSL_LDR - total;

            //Console.WriteLine("正向图像videoBW：{0}", videoBW);
            //Console.WriteLine("正向GPIO：{0}", forwardGPIO);
            //Console.WriteLine("反向GPIO：{0}", reverseGPIO);
            //Console.WriteLine("反向I2C(透传模式) A：{0}", reverseI2CAPass);
            //Console.WriteLine("反向I2C(非透传模式) A：{0}", reverseI2CANonPass);
            //Console.WriteLine("反向SPI A：{0}", reverseSPIA);
            //Console.WriteLine("反向I2C(透传模式) B：{0}", reverseI2CBPass);
            //Console.WriteLine("反向I2C(非透传模式) B：{0}", reverseI2CBNonPass);
            //Console.WriteLine("反向SPI B：{0}", reverseSPIB);
            //Console.WriteLine("反向I2C(透传模式) C：{0}", reverseI2CCPass);
            //Console.WriteLine("反向I2C(非透传模式) C：{0}", reverseI2CCNonPass);
            //Console.WriteLine("反向SPI C：{0}", reverseSPIC);
            //Console.WriteLine("Total：{0}", total);
            //Console.WriteLine("Free：{0}", free);

            if(total > GMSL_LDR)
            {
                uiLabel_HSMT.Text = "HSMT Bandwidth Exceed";
                uiLedBulb_bandwidth.Color = Color.Red;
            }
            else
            {
                uiLabel_HSMT.Text = "Meets HSMT Bandwidth";
                uiLedBulb_bandwidth.Color = Color.FromArgb(192, 255, 192);
            }
            
            UpdateDataGridViewValue("Video BW", videoBW);   
            UpdateDataGridViewValue("正向GPIO", forwardGPIO);
            UpdateDataGridViewValue("反向GPIO", reverseGPIO);
            //UpdateDataGridViewValue("反向I2C(透传模式)", reverseI2CPass);
            //UpdateDataGridViewValue("反向I2C(非透传模式)", reverseI2CNonPass);
            //UpdateDataGridViewValue("反向SPI", reverseSPI);
            UpdateDataGridViewValue("反向I2C A(透传模式)", reverseI2CAPass);
            UpdateDataGridViewValue("反向I2C A(非透传模式)", reverseI2CANonPass);
            UpdateDataGridViewValue("反向SPI A", reverseSPIA);
            UpdateDataGridViewValue("反向I2C B(透传模式)", reverseI2CBPass);
            UpdateDataGridViewValue("反向I2C B(非透传模式)", reverseI2CBNonPass);
            UpdateDataGridViewValue("反向SPI B", reverseSPIB);
            UpdateDataGridViewValue("反向I2C C(透传模式)", reverseI2CCPass);
            UpdateDataGridViewValue("反向I2C C(非透传模式)", reverseI2CCNonPass);
            UpdateDataGridViewValue("反向SPI C", reverseSPIC);
            
            UpdateDataGridViewValue("Total", total);
            UpdateDataGridViewValue("Total CC BW", totalCC);
            UpdateDataGridViewValue("Free", free);

            //UpdateChart_TotalBandwidth_Value("Free", free);
            UpdateChart_TotalBandwidth_Value("Video A", videoBW);
            UpdateChart_TotalBandwidth_Value("正向GPIO", forwardGPIO);
            UpdateChart_TotalBandwidth_Value("反向GPIO", reverseGPIO);
            UpdateChart_TotalBandwidth_Value("反向I2C A(透传)", reverseI2CAPass);
            UpdateChart_TotalBandwidth_Value("反向I2C A(非透传)", reverseI2CANonPass);
            UpdateChart_TotalBandwidth_Value("反向SPI A", reverseSPIA);
            UpdateChart_TotalBandwidth_Value("反向I2C B(透传)", reverseI2CBPass);
            UpdateChart_TotalBandwidth_Value("反向I2C B(非透传)", reverseI2CBNonPass);
            UpdateChart_TotalBandwidth_Value("反向SPI B", reverseSPIB);
            UpdateChart_TotalBandwidth_Value("反向I2C C(透传)", reverseI2CCPass);
            UpdateChart_TotalBandwidth_Value("反向I2C C(非透传)", reverseI2CCNonPass);
            UpdateChart_TotalBandwidth_Value("反向SPI C", reverseSPIC);

            UpdateChart_CameraBandwidth_Value("Pipe X", videoBWX);
            UpdateChart_CameraBandwidth_Value("Pipe Y", videoBWY);
            UpdateChart_CameraBandwidth_Value("Pipe Z", videoBWZ);
            UpdateChart_CameraBandwidth_Value("Pipe U", videoBWU);
        }

        private void UIInit()
        {
            childBandwidthCamera = new BandwidthCamera(this);
            uiTabControl_BandwidthCal.AddPage(childBandwidthCamera);
            this.tabPage_BandwidthCamera.Text = "Camera";
            childBandwidthConChannel = new BandwidthConChannel(this);
            uiTabControl_BandwidthCal.AddPage(childBandwidthConChannel);
            this.tabPage_BandwidthConChannel.Text = "Control Channel";
            //uiComboBox_GMSLRate.SelectedItem = "12000";
            PlotTotalBandwidth();
            PlotCameraBandwidth();
            uiComboBox_GMSLRate.SelectedIndex = 4; //需要在Plot之后
        }

        private void PlotTotalBandwidth()
        {
            //标题
            chart_TotalBandwidth.Titles.Add("Total Bandwidth Usage");
            chart_TotalBandwidth.Titles[0].Alignment = ContentAlignment.TopCenter;
            chart_TotalBandwidth.Titles[0].ForeColor = Color.Black;
            chart_TotalBandwidth.Titles[0].Font = new Font("微软细黑", 12f, FontStyle.Regular);

            // 设置图例位置为底部左对齐
            chart_TotalBandwidth.Legends["Legend1"].Docking = Docking.Bottom;
            chart_TotalBandwidth.Legends["Legend1"].Alignment = StringAlignment.Near;
            chart_TotalBandwidth.Legends["Legend1"].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(249)))), ((int)(((byte)(255)))));

            //chart背景颜色
            chart_TotalBandwidth.BackColor = Color.Transparent;
            //区域背景颜色
            chart_TotalBandwidth.ChartAreas[0].BackColor = Color.Transparent;
            //X轴字体角度
            //chart_TotalBandwidth.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
            //3D显示
            //chart_TotalBandwidth.ChartAreas[0].Area3DStyle.Enable3D = true;         
            // 设置Y轴最大值
            chart_TotalBandwidth.ChartAreas[0].AxisY.Maximum = GMSL_LDR;

            //添加数据
            double[] value1 = new double[] { 0 };
            double[] value2 = new double[] { 0 }; // 第二组数据
            double[] value3 = new double[] { 0 };
            double[] value4 = new double[] { 0 };
            double[] value5 = new double[] { 0 };
            double[] value6 = new double[] { 0 };
            double[] value7 = new double[] { 0 };
            double[] value8 = new double[] { 0 };
            double[] value9 = new double[] { 0 };
            double[] value10 = new double[] { 0 };
            double[] value11 = new double[] { 0 };
            double[] value12 = new double[] { 0 };
            double[] value13 = new double[] { 0 };
            string[] name = new string[] { "BW" };
            chart_TotalBandwidth.Series.Add("Free");
            chart_TotalBandwidth.Series.Add("Video A"); // 添加第二组数据
            chart_TotalBandwidth.Series.Add("正向GPIO");
            chart_TotalBandwidth.Series.Add("反向GPIO");
            chart_TotalBandwidth.Series.Add("反向I2C A(透传)");
            chart_TotalBandwidth.Series.Add("反向I2C A(非透传)");
            chart_TotalBandwidth.Series.Add("反向SPI A");
            chart_TotalBandwidth.Series.Add("反向I2C B(透传)");
            chart_TotalBandwidth.Series.Add("反向I2C B(非透传)");
            chart_TotalBandwidth.Series.Add("反向SPI B");
            chart_TotalBandwidth.Series.Add("反向I2C C(透传)");
            chart_TotalBandwidth.Series.Add("反向I2C C(非透传)");
            chart_TotalBandwidth.Series.Add("反向SPI C");
            //chart_TotalBandwidth.Series.Add("SYNC");
            chart_TotalBandwidth.Series[1].Points.DataBindXY(name, value1);
            chart_TotalBandwidth.Series[2].Points.DataBindXY(name, value2); // 绑定第二组数据
            chart_TotalBandwidth.Series[3].Points.DataBindXY(name, value3);
            chart_TotalBandwidth.Series[4].Points.DataBindXY(name, value4);
            chart_TotalBandwidth.Series[5].Points.DataBindXY(name, value5);
            chart_TotalBandwidth.Series[6].Points.DataBindXY(name, value6);
            chart_TotalBandwidth.Series[7].Points.DataBindXY(name, value7);
            chart_TotalBandwidth.Series[8].Points.DataBindXY(name, value8);
            chart_TotalBandwidth.Series[9].Points.DataBindXY(name, value9);
            chart_TotalBandwidth.Series[10].Points.DataBindXY(name, value10);
            chart_TotalBandwidth.Series[11].Points.DataBindXY(name, value11);
            chart_TotalBandwidth.Series[12].Points.DataBindXY(name, value12);
            chart_TotalBandwidth.Series[13].Points.DataBindXY(name, value13);

            // 修改图例中系列的名称
            double Totalvalue = value1[0] + value2[0] + value3[0] + value4[0];
            //chart_TotalBandwidth.Series[1].LegendText = "Free " + (value1[0] / Totalvalue * 100).ToString("0.00") + "%";
            //chart_TotalBandwidth.Series[2].LegendText = "Video A " + (value2[0] / Totalvalue * 100).ToString("0.00") + "%";
            //chart_TotalBandwidth.Series[3].LegendText = "GPIO " + (value3[0] / Totalvalue * 100).ToString("0.00") + "%";
            //chart_TotalBandwidth.Series[4].LegendText = "SYNC " + (value4[0] / Totalvalue * 100).ToString("0.00") + "%";
            chart_TotalBandwidth.Series[1].LegendText = "Free " + (value1[0]).ToString("0.00");
            chart_TotalBandwidth.Series[2].LegendText = "Video A " + (value2[0]).ToString("0.00");
            chart_TotalBandwidth.Series[3].LegendText = "正向GPIO " + (value3[0]).ToString("0.00");
            chart_TotalBandwidth.Series[4].LegendText = "反向GPIO " + (value4[0]).ToString("0.00");
            chart_TotalBandwidth.Series[5].LegendText = "反向I2C A(透传) " + (value5[0]).ToString("0.00");
            chart_TotalBandwidth.Series[6].LegendText = "反向I2C A(非透传) " + (value6[0]).ToString("0.00");
            chart_TotalBandwidth.Series[7].LegendText = "反向SPI A " + (value7[0]).ToString("0.00");
            chart_TotalBandwidth.Series[8].LegendText = "反向I2C B(透传) " + (value8[0]).ToString("0.00");
            chart_TotalBandwidth.Series[9].LegendText = "反向I2C B(非透传) " + (value9[0]).ToString("0.00");
            chart_TotalBandwidth.Series[10].LegendText = "反向SPI B " + (value10[0]).ToString("0.00");
            chart_TotalBandwidth.Series[11].LegendText = "反向I2C C(透传) " + (value11[0]).ToString("0.00");
            chart_TotalBandwidth.Series[12].LegendText = "反向I2C C(非透传) " + (value12[0]).ToString("0.00");
            chart_TotalBandwidth.Series[13].LegendText = "反向SPI C " + (value13[0]).ToString("0.00");
            //chart_TotalBandwidth.Series[4].LegendText = "SYNC " + (value4[0]).ToString("0.00");

            //类型配置，这里决定了图是竖直的(Column)还是横向的(Bar)，或者是其他 饼图(Pie)
            chart_TotalBandwidth.Series[0].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[1].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[2].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[3].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[4].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[5].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[6].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[7].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[8].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[9].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[10].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[11].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[12].ChartType = SeriesChartType.StackedBar;
            chart_TotalBandwidth.Series[13].ChartType = SeriesChartType.StackedBar;

            //X轴坐标改为字符型
            //chart_TotalBandwidth.Series[1].XValueType = ChartValueType.String;
            //chart_TotalBandwidth.Series[2].XValueType = ChartValueType.String;
            //显示图例
            chart_TotalBandwidth.Series[0].IsVisibleInLegend = false;
            chart_TotalBandwidth.Series[1].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[2].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[3].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[4].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[5].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[6].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[7].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[8].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[9].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[10].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[11].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[12].IsVisibleInLegend = true;
            chart_TotalBandwidth.Series[13].IsVisibleInLegend = true;
            //柱体上是否显示具体数值
            //chart_TotalBandwidth.Series[1].IsValueShownAsLabel = true;
            //chart_TotalBandwidth.Series[2].IsValueShownAsLabel = true;
            //chart_TotalBandwidth.Series[3].IsValueShownAsLabel = true;
            //chart_TotalBandwidth.Series[4].IsValueShownAsLabel = true;
            //柱体上数值字体颜色
            chart_TotalBandwidth.Series[1].LabelForeColor = Color.White;
            chart_TotalBandwidth.Series[2].LabelForeColor = Color.White;
            chart_TotalBandwidth.Series[3].LabelForeColor = Color.White;
            chart_TotalBandwidth.Series[4].LabelForeColor = Color.White;
            //柱体上数值字体大小
            chart_TotalBandwidth.Series[1].Font = new Font("宋体", 10f, FontStyle.Bold);//柱体上显示数值的字体大小
            chart_TotalBandwidth.Series[2].Font = new Font("宋体", 10f, FontStyle.Bold);
            chart_TotalBandwidth.Series[3].Font = new Font("宋体", 10f, FontStyle.Bold);
            chart_TotalBandwidth.Series[4].Font = new Font("宋体", 10f, FontStyle.Bold);
            //各数据柱体颜色风格
            //chart_TotalBandwidth.Series[1].Palette = ChartColorPalette.Pastel;
            //chart_TotalBandwidth.Series[2].Palette = ChartColorPalette.Pastel;
        }

        private void PlotCameraBandwidth()
        {
            //标题
            chart_CameraBandwidth.Titles.Add("Camera Bandwidth Usage");
            chart_CameraBandwidth.Titles[0].Alignment = ContentAlignment.TopCenter;
            chart_CameraBandwidth.Titles[0].ForeColor = Color.Black;
            chart_CameraBandwidth.Titles[0].Font = new Font("微软细黑", 12f, FontStyle.Regular);

            // 设置图例位置为底部左对齐
            chart_CameraBandwidth.Legends["Legend1"].Docking = Docking.Bottom;
            chart_CameraBandwidth.Legends["Legend1"].Alignment = StringAlignment.Near;
            chart_CameraBandwidth.Legends["Legend1"].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(249)))), ((int)(((byte)(255)))));

            //chart背景颜色
            chart_CameraBandwidth.BackColor = Color.Transparent;
            //区域背景颜色
            chart_CameraBandwidth.ChartAreas[0].BackColor = Color.Transparent;
            //X轴字体角度
            //chart_CameraBandwidth.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
            //3D显示
            //chart_CameraBandwidth.ChartAreas[0].Area3DStyle.Enable3D = true;         
            // 设置Y轴最大值
            chart_CameraBandwidth.ChartAreas[0].AxisY.Maximum = GMSL_LDR;

            //添加数据
            double[] valueX = new double[] { 0 };
            double[] valueY = new double[] { 0 }; // 第二组数据
            double[] valueZ = new double[] { 0 };
            double[] valueU = new double[] { 0 };
            string[] name = new string[] { "BW" };
            chart_CameraBandwidth.Series.Add("Pipe X");
            chart_CameraBandwidth.Series.Add("Pipe Y"); // 添加第二组数据
            chart_CameraBandwidth.Series.Add("Pipe Z");
            chart_CameraBandwidth.Series.Add("Pipe U");
            chart_CameraBandwidth.Series[1].Points.DataBindXY(name, valueX);
            chart_CameraBandwidth.Series[2].Points.DataBindXY(name, valueY); // 绑定第二组数据
            chart_CameraBandwidth.Series[3].Points.DataBindXY(name, valueZ);
            chart_CameraBandwidth.Series[4].Points.DataBindXY(name, valueU);

            // 修改图例中系列的名称
            double TotalCameravalue = valueX[0] + valueY[0] + valueZ[0] + valueU[0];
            //chart_CameraBandwidth.Series[1].LegendText = "Pipe X " + (valueX[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            //chart_CameraBandwidth.Series[2].LegendText = "Pipe Y " + (valueY[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            //chart_CameraBandwidth.Series[3].LegendText = "Pipe Z " + (valueZ[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            //chart_CameraBandwidth.Series[4].LegendText = "Pipe U " + (valueU[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            chart_CameraBandwidth.Series[1].LegendText = "Pipe X " + (valueX[0]).ToString("0.00");
            chart_CameraBandwidth.Series[2].LegendText = "Pipe Y " + (valueY[0]).ToString("0.00");
            chart_CameraBandwidth.Series[3].LegendText = "Pipe Z " + (valueZ[0]).ToString("0.00");
            chart_CameraBandwidth.Series[4].LegendText = "Pipe U " + (valueU[0]).ToString("0.00");

            //类型配置，这里决定了图是竖直的(Column)还是横向的(Bar)，或者是其他 饼图(Pie)
            chart_CameraBandwidth.Series[0].ChartType = SeriesChartType.StackedBar;
            chart_CameraBandwidth.Series[1].ChartType = SeriesChartType.StackedBar;
            chart_CameraBandwidth.Series[2].ChartType = SeriesChartType.StackedBar;
            chart_CameraBandwidth.Series[3].ChartType = SeriesChartType.StackedBar;
            chart_CameraBandwidth.Series[4].ChartType = SeriesChartType.StackedBar;

            //X轴坐标改为字符型
            //chart_CameraBandwidth.Series[1].XValueType = ChartValueType.String;
            //chart_CameraBandwidth.Series[2].XValueType = ChartValueType.String;
            //显示图例
            chart_CameraBandwidth.Series[0].IsVisibleInLegend = false;
            chart_CameraBandwidth.Series[1].IsVisibleInLegend = true;
            chart_CameraBandwidth.Series[2].IsVisibleInLegend = true;
            chart_CameraBandwidth.Series[3].IsVisibleInLegend = true;
            chart_CameraBandwidth.Series[4].IsVisibleInLegend = true;
            //柱体上是否显示具体数值
            //chart_CameraBandwidth.Series[1].IsValueShownAsLabel = true;
            //chart_CameraBandwidth.Series[2].IsValueShownAsLabel = true;
            //chart_CameraBandwidth.Series[3].IsValueShownAsLabel = true;
            //chart_CameraBandwidth.Series[4].IsValueShownAsLabel = true;
            //柱体上数值字体颜色
            chart_CameraBandwidth.Series[1].LabelForeColor = Color.White;
            chart_CameraBandwidth.Series[2].LabelForeColor = Color.White;
            chart_CameraBandwidth.Series[3].LabelForeColor = Color.White;
            chart_CameraBandwidth.Series[4].LabelForeColor = Color.White;
            //柱体上数值字体大小
            chart_CameraBandwidth.Series[1].Font = new Font("宋体", 10f, FontStyle.Bold);//柱体上显示数值的字体大小
            chart_CameraBandwidth.Series[2].Font = new Font("宋体", 10f, FontStyle.Bold);
            chart_CameraBandwidth.Series[3].Font = new Font("宋体", 10f, FontStyle.Bold);
            chart_CameraBandwidth.Series[4].Font = new Font("宋体", 10f, FontStyle.Bold);
            //各数据柱体颜色风格
            //chart_CameraBandwidth.Series[1].Palette = ChartColorPalette.Pastel;
            //chart_CameraBandwidth.Series[2].Palette = ChartColorPalette.Pastel;
        }

        private void DGVCalInit()
        {
            //dataGridView_Cal.
            dataGridView_Cal.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView_Cal.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView_Cal.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView_Cal.RowHeadersVisible = false;
            dataGridView_Cal.AllowUserToAddRows = false;
        }

        // 定义自定义数据对象(List)
        public class DataObject
        {
            public string Name { get; set; }
            public double Mbps { get; set; }
        }

        // 使用 List 数据源
        private List<DataObject> dataList = new List<DataObject>();

        // 使用 DataTable 数据源
        private DataTable dataTable = new DataTable();

        private void BindData()
        {
            // 为 DataTable 添加列
            dataTable.Columns.Add(" ", typeof(string));
            dataTable.Columns.Add("Mbps", typeof(double));

            // 添加数据
            AddData();

            // 将数据源绑定到 DataGridView
            //dataGridView_Cal.DataSource = dataList; // 如果使用 List
            dataGridView_Cal.DataSource = dataTable; // 如果使用 DataTable
        }

        // 添加数据
        private void AddData()
        {
            // 使用 List 添加数据
            dataList.Add(new DataObject { Name = "Item 1", Mbps = 100 });
            dataList.Add(new DataObject { Name = "Item 2", Mbps = 200 });
            dataList.Add(new DataObject { Name = "Item 3", Mbps = 300 });

            // 使用 DataTable 添加数据
            dataTable.Rows.Add("Total", 0);

            dataTable.Rows.Add("Video BW", 0);
            dataTable.Rows.Add("Total CC BW", 0);
            dataTable.Rows.Add("正向GPIO", 0);
            dataTable.Rows.Add("反向GPIO", 0);
            dataTable.Rows.Add("反向I2C A(透传模式)", 0);
            dataTable.Rows.Add("反向I2C A(非透传模式)", 0);
            dataTable.Rows.Add("反向SPI A", 0);
            dataTable.Rows.Add("反向I2C B(透传模式)", 0);
            dataTable.Rows.Add("反向I2C B(非透传模式)", 0);
            dataTable.Rows.Add("反向SPI B", 0);
            dataTable.Rows.Add("反向I2C C(透传模式)", 0);
            dataTable.Rows.Add("反向I2C C(非透传模式)", 0);
            dataTable.Rows.Add("反向SPI C", 0);
            
            dataTable.Rows.Add("SYNC", 0);
            dataTable.Rows.Add("Audio A", 0);
            dataTable.Rows.Add("Info Audio A", 0);
            dataTable.Rows.Add("GPIO (PWM)", 0);
            dataTable.Rows.Add("SPI", 0);
            dataTable.Rows.Add("RGMII", 0);
            dataTable.Rows.Add("I2C A", 0);
            dataTable.Rows.Add("I2C B", 0);
            dataTable.Rows.Add("I2C C", 0);
            dataTable.Rows.Add("HDCP", 0);
            dataTable.Rows.Add("Free", 0);
        }

        //要更新dataGridView_Cal的值，您需要更新dataTable中的数据，因为DataGridView会自动反映其数据源中的更改
        private void UpdateDataGridViewValue(string name, double newMbps)
        {
            // 遍历所有行以找到具有指定Name的行  
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.Field<string>(" ") == name)
                {
                    string formattedNewMbps = newMbps.ToString("F3"); // 使用"F3"格式说明符
                    row["Mbps"] = formattedNewMbps; // 使用索引器更新Mbps列的值  
                    break; // 找到后退出循环  
                }
            }
        }

        private void UpdateChart_TotalBandwidth_Value(string pipe, double value)
        {
            // 找到Series对象  
            Series freeSeries = chart_TotalBandwidth.Series[pipe];
            if (freeSeries != null && freeSeries.Points.Count > 0)
            {
                // 修改第一个数据点的Y值  
                freeSeries.Points[0].YValues[0] = value;

                UpdateChart_TotalBandwidth();
            }
        }

        private void UpdateChart_TotalBandwidth()
        {
            //chart_TotalBandwidth.Series[1].LegendText = "Free " + (chart_TotalBandwidth.Series["Free"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[1].LegendText = "Free " + (free).ToString("0.00");
            chart_TotalBandwidth.Series[2].LegendText = "Video A " + (chart_TotalBandwidth.Series["Video A"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[3].LegendText = "正向GPIO " + (chart_TotalBandwidth.Series["正向GPIO"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[4].LegendText = "反向GPIO " + (chart_TotalBandwidth.Series["反向GPIO"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[5].LegendText = "反向I2C A(透传) " + (chart_TotalBandwidth.Series["反向I2C A(透传)"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[6].LegendText = "反向I2C A(非透传) " + (chart_TotalBandwidth.Series["反向I2C A(非透传)"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[7].LegendText = "反向SPI A " + (chart_TotalBandwidth.Series["反向SPI A"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[8].LegendText = "反向I2C B(透传) " + (chart_TotalBandwidth.Series["反向I2C B(透传)"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[9].LegendText = "反向I2C B(非透传) " + (chart_TotalBandwidth.Series["反向I2C B(非透传)"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[10].LegendText = "反向SPI B " + (chart_TotalBandwidth.Series["反向SPI B"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[11].LegendText = "反向I2C C(透传) " + (chart_TotalBandwidth.Series["反向I2C C(透传)"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[12].LegendText = "反向I2C C(非透传) " + (chart_TotalBandwidth.Series["反向I2C C(非透传)"].Points[0].YValues[0]).ToString("0.00");
            chart_TotalBandwidth.Series[13].LegendText = "反向SPI C " + (chart_TotalBandwidth.Series["反向SPI C"].Points[0].YValues[0]).ToString("0.00");
            //chart_TotalBandwidth.Series[4].LegendText = "SYNC " + (chart_TotalBandwidth.Series["SYNC"].Points[0].YValues[0]).ToString("0.00");

            chart_TotalBandwidth.Series[1].LabelFormat = "F2"; // F2表示固定点表示法，保留2位小数 
            chart_TotalBandwidth.Series[2].LabelFormat = "F2";
            chart_TotalBandwidth.Series[3].LabelFormat = "F2";
            chart_TotalBandwidth.Series[4].LabelFormat = "F2";
            chart_TotalBandwidth.Series[5].LabelFormat = "F2";
            chart_TotalBandwidth.Series[6].LabelFormat = "F2";
            chart_TotalBandwidth.Series[7].LabelFormat = "F2";
            chart_TotalBandwidth.Series[8].LabelFormat = "F2";
            chart_TotalBandwidth.Series[9].LabelFormat = "F2";
            chart_TotalBandwidth.Series[10].LabelFormat = "F2";
            chart_TotalBandwidth.Series[11].LabelFormat = "F2";
            chart_TotalBandwidth.Series[12].LabelFormat = "F2";
            chart_TotalBandwidth.Series[13].LabelFormat = "F2";

            // 刷新图表以显示更改  
            chart_TotalBandwidth.Invalidate();
        }

        private void UpdateChart_CameraBandwidth_Value(string pipe, double value)
        {
            // 找到Pipe的Series对象  
            Series pipeXSeries = chart_CameraBandwidth.Series[pipe];
            if (pipeXSeries != null && pipeXSeries.Points.Count > 0)
            {
                // 修改第一个数据点的Y值  
                pipeXSeries.Points[0].YValues[0] = value;

                UpdateChart_CameraBandwidth();
            }
        }

        private void UpdateChart_CameraBandwidth()
        {
            // 重新计算并更新图例文本  
            double TotalCameravalue = chart_CameraBandwidth.Series["Pipe X"].Points[0].YValues[0] + // Pipe X的新值  
                                      chart_CameraBandwidth.Series["Pipe Y"].Points[0].YValues[0] +
                                      chart_CameraBandwidth.Series["Pipe Z"].Points[0].YValues[0] +
                                      chart_CameraBandwidth.Series["Pipe U"].Points[0].YValues[0];
            //chart_CameraBandwidth.Series[1].LegendText = "Pipe X " + (chart_CameraBandwidth.Series["Pipe X"].Points[0].YValues[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            //chart_CameraBandwidth.Series[2].LegendText = "Pipe Y " + (chart_CameraBandwidth.Series["Pipe Y"].Points[0].YValues[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            //chart_CameraBandwidth.Series[3].LegendText = "Pipe Z " + (chart_CameraBandwidth.Series["Pipe Z"].Points[0].YValues[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            //chart_CameraBandwidth.Series[4].LegendText = "Pipe U " + (chart_CameraBandwidth.Series["Pipe U"].Points[0].YValues[0] / TotalCameravalue * 100).ToString("0.00") + "%";
            chart_CameraBandwidth.Series[1].LegendText = "Pipe X " + (chart_CameraBandwidth.Series["Pipe X"].Points[0].YValues[0]).ToString("0.00");
            chart_CameraBandwidth.Series[2].LegendText = "Pipe Y " + (chart_CameraBandwidth.Series["Pipe Y"].Points[0].YValues[0]).ToString("0.00");
            chart_CameraBandwidth.Series[3].LegendText = "Pipe Z " + (chart_CameraBandwidth.Series["Pipe Z"].Points[0].YValues[0]).ToString("0.00");
            chart_CameraBandwidth.Series[4].LegendText = "Pipe U " + (chart_CameraBandwidth.Series["Pipe U"].Points[0].YValues[0]).ToString("0.00");

            chart_CameraBandwidth.Series[1].LabelFormat = "F2"; // F2表示固定点表示法，保留2位小数 
            chart_CameraBandwidth.Series[2].LabelFormat = "F2";
            chart_CameraBandwidth.Series[3].LabelFormat = "F2";
            chart_CameraBandwidth.Series[4].LabelFormat = "F2";

            // 刷新图表以显示更改  
            chart_CameraBandwidth.Invalidate();
        }

        private void uiComboBox_GMSLRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (uiComboBox_GMSLRate.SelectedIndex)
            {
                case 0:
                    GMSL_LDR = 2000;
                    break;
                case 1:
                    GMSL_LDR = 3200;
                    break;
                case 2:
                    GMSL_LDR = 4000;
                    break;
                case 3:
                    GMSL_LDR = 6400;
                    break;
                case 4:
                    GMSL_LDR = 8000;
                    break;
                case 5:
                    GMSL_LDR = 12800;
                    break;
            }
            chart_TotalBandwidth.ChartAreas[0].AxisY.Maximum = GMSL_LDR;
            chart_CameraBandwidth.ChartAreas[0].AxisY.Maximum = GMSL_LDR;
            CalculatorAll();
        }
    }
}
